package com.example.flutter_demosj;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

public class MainActivity  extends  FlutterActivity {


    BinaryMessenger getFlutterView(){
        return getFlutterEngine().getDartExecutor().getBinaryMessenger();
    }

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        //创建与调用方标识符一样的方法通道
        new MethodChannel(getFlutterView(), "samples.chenhang/utils").setMethodCallHandler(
                //设置方法处理回调
                new MethodChannel.MethodCallHandler() {
                    //响应方法请求
                    @Override
                    public void onMethodCall(MethodCall call, MethodChannel.Result result) {
                        Log.d("flutter", "onMethodCall");
                        Log.d("flutter", call.method);
                        //判断方法名是否支持
                        if(call.method.equals("openAppMarket")) {
                            try {
                                //应用市场URI
                                Uri uri = Uri.parse("market://details?id=com.hangchen.example.flutter_module_page.host");
                                Log.d("flutter", uri.toString());

                                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                //打开应用市场
                                MainActivity.this.startActivity(intent);
                                //返回处理结果
                                result.success(0);
                            } catch (Exception e) {
                                e.printStackTrace();
                                Log.d("flutter",e.toString());
                                //打开应用市场出现异常
                                result.error("UNAVAILABLE", "没有安装应用市场", null);
                            }
                        }else {
                            //方法名暂不支持
                            result.notImplemented();
                        }
                    }
                });
    }
}
